package com.yxjx.duoxue.global;

import java.util.ArrayList;
import java.util.Iterator;

import android.content.Context;

import com.baidu.location.LocationClient;
import com.yxjx.duoxue.course.CourseOrderKidsOption;
import com.yxjx.duoxue.course.datamodel.HomeAllAdData;
import com.yxjx.duoxue.datamodel.DMArea;
import com.yxjx.duoxue.datamodel.DMBaiduLocation;
import com.yxjx.duoxue.datamodel.DMCourseCategory;
import com.yxjx.duoxue.datamodel.DMKeyword;
import com.yxjx.duoxue.datamodel.DMReportErrorItem;
import com.yxjx.duoxue.datamodel.DMSortBy;
import com.yxjx.duoxue.datamodel.config.ConfigRefundReasonDTS;
import com.yxjx.duoxue.datamodel.config.ConfigRefundTypeDTS;
import com.yxjx.duoxue.datamodel.course.DMCourse;
import com.yxjx.duoxue.message.UnreadStatus;
import com.yxjx.duoxue.network.UserLoginResponse;
import com.yxjx.duoxue.payment.ClientUserKidsDTS;
import com.yxjx.duoxue.utils.UIHelper;

public class Globals {
	
	private static Globals sInstance;
	
	/**
	 * 清除unread的状态
	 */
	public static void clearNewMessages(Context ctx) {
		UnreadStatus unread = Globals.getInstance(ctx).getUnread();
		if (unread != null) {
			unread.setHasNewMessage(false);
		}
	}
	
	public static synchronized Globals getInstance(Context ctx) {
		if (sInstance == null) {
			sInstance = new Globals(ctx);
		}
		return sInstance;
	}
	
	// 登陆信息
	private UserLoginResponse ulRsp;
	
	// 报错列表
	private ArrayList<DMReportErrorItem> errorList;
	
	// 学生信息。
	private ClientUserKidsDTS kidInfo;
	// 订单学生选项
	private CourseOrderKidsOption kidOption;
	
	private HomeAllAdData mAds;

	private ArrayList<ArrayList<DMArea>> mAreas;

	private Context mContext;
	private ArrayList<ArrayList<DMCourseCategory>> mCourseTypes; // 用于在查询条件处显示的二维数组。

	private ArrayList<DMCourseCategory> mDetailedCourseTypes; // 详细的全部课程分类页面（嵌套）。
	private ArrayList<DMCourseCategory> mHomeCourseCategories;
	
	private ArrayList<DMKeyword> mKeywords;
	// 当前位置
	private DMBaiduLocation mLocation;

	private LocationClient mLocationClient;
	private ArrayList<DMCourse> mRecommendCourses;

	private ArrayList<String> mSearchHistory;
	
	// 课程和学校列表的排序方式：
	private ArrayList<DMSortBy> mSortBysCourse;

	private ArrayList<DMSortBy> mSortBysSchool;
	private ArrayList<ConfigRefundReasonDTS> refundReasons;

	// refund 退款方式和退款原因列表
	private ArrayList<ConfigRefundTypeDTS> refundTypes;

	// 是否存在未读消息
	private UnreadStatus unread;
	// 是否正在弹出用户评分的dialog
	private boolean isShowingDialog = false;

	private Globals(Context context) {
		mContext = context;
	}

	public void addSearchHistoryItem(String text) {
		if (mSearchHistory == null || UIHelper.isEmpty(text)) {
			return;
		}
		
		Iterator<String> iterator = mSearchHistory.iterator();
		while (iterator.hasNext()) {
			String item = iterator.next();
			if (item.equals(text)) {
				iterator.remove();
			}
		}
		
		mSearchHistory.add(0, text);
	}

	public HomeAllAdData getAllAdsData() {
		return mAds;
	}

	public ArrayList<ArrayList<DMArea>> getAreasInfo() {
		return mAreas;
	}

	public ArrayList<ArrayList<DMCourseCategory>> getCourseTpes() {
		return mCourseTypes;
	}

	public ArrayList<DMCourseCategory> getDetailedCourseTypes() {
		return mDetailedCourseTypes;
	}

	public ArrayList<DMReportErrorItem> getErrorList() {
		return errorList;
	}
	
	public ArrayList<DMCourseCategory> getHomeCourseCategories() {
		return mHomeCourseCategories;
	}

//	public synchronized String getDeviceId() {
//		return deviceId;
//	}

	public ArrayList<DMKeyword> getKeywords() {
		return mKeywords;
	}

	public ClientUserKidsDTS getKidInfo() {
		return kidInfo;
	}

	public CourseOrderKidsOption getKidOption() {
		return kidOption;
	}

	/** 
	 * 获取内存中保存的当前位置信息。
	 * 其他模块使用DMBaiduLocation.getLocation() 代替。
	 * @return
	 */
	public DMBaiduLocation getLocation() {
		return mLocation;
	}

	public LocationClient getLocationClient() {
		return mLocationClient;
	}

	public ArrayList<DMCourse> getRecommendCourses() {
		return mRecommendCourses;
	}

	public ArrayList<ConfigRefundReasonDTS> getRefundReasons() {
		return refundReasons;
	}
	public ArrayList<ConfigRefundTypeDTS> getRefundTypes() {
		return refundTypes;
	}
	public ArrayList<String> getSearchHistory() {
		return mSearchHistory;
	}
	public ArrayList<DMSortBy> getSortBysCourse() {
		return mSortBysCourse;
	}

	public ArrayList<DMSortBy> getSortBysSchool() {
		return mSortBysSchool;
	}
	public UnreadStatus getUnread() {
		return unread;
	}
	
	public void setAllAdsData(HomeAllAdData mAds) {
		this.mAds = mAds;
	}
	
	/**
	 * When loading app, set it.
	 * @param types
	 */
	public void setCourseTypes(ArrayList<ArrayList<DMCourseCategory>> types) {
		mCourseTypes = types;
	}
	public void setDetailedCourseTypes(ArrayList<DMCourseCategory> mDetailedCourseTypes) {
		this.mDetailedCourseTypes = mDetailedCourseTypes;
	}
	public void setErrorList(ArrayList<DMReportErrorItem> errorList) {
		this.errorList = errorList;
	}
	
	public void setHomeCourseCategories(ArrayList<DMCourseCategory> mHomeCourseCategories) {
		this.mHomeCourseCategories = mHomeCourseCategories;
	}
	public void setKeywords(ArrayList<DMKeyword> mKeywords) {
		this.mKeywords = mKeywords;
	}
	
	public void setKidInfo(ClientUserKidsDTS kidInfo) {
		this.kidInfo = kidInfo;
	}
	
	public void setKidOption(CourseOrderKidsOption kidOption) {
		this.kidOption = kidOption;
	}
	public void setLocation(DMBaiduLocation mLocation) {
		this.mLocation = mLocation;
	}
	public void setLocationClient(LocationClient mLocationClient) {
		this.mLocationClient = mLocationClient;
	}
	/**
	 * When loading app, set it.
	 * @param areas
	 */
	public void setmAreasInfo(ArrayList<ArrayList<DMArea>> areas) {
		mAreas = areas;
	}
	public void setRecommendCourses(ArrayList<DMCourse> mRecommendCourses) {
		this.mRecommendCourses = mRecommendCourses;
	}
	
	public void setRefundReasons(ArrayList<ConfigRefundReasonDTS> refundReasons) {
		this.refundReasons = refundReasons;
	}
	public void setRefundTypes(ArrayList<ConfigRefundTypeDTS> refundTypes) {
		this.refundTypes = refundTypes;
	}
	
	public void setSearchHistory(ArrayList<String> mSearchHistory) {
		this.mSearchHistory = mSearchHistory;
	}
	
	public void setSortBysCourse(ArrayList<DMSortBy> mSortBysCourse) {
		this.mSortBysCourse = mSortBysCourse;
	}

	public void setSortBysSchool(ArrayList<DMSortBy> mSortBysSchool) {
		this.mSortBysSchool = mSortBysSchool;
	}

	public void setUnread(UnreadStatus unread) {
		this.unread = unread;
	}

	public boolean isShowingDialog() {
		return isShowingDialog;
	}

	public void setShowingDialog(boolean isShowingDialog) {
		this.isShowingDialog = isShowingDialog;
	}

	public UserLoginResponse getUlRsp() {
		return ulRsp;
	}

	public void setUlRsp(UserLoginResponse ulRsp) {
		this.ulRsp = ulRsp;
	}
}
